package com.breezelin.freshmarket.fragments;

/**
 * Created by Breeze Lin
 * 2016/9/26 14:16
 * 1linyufeng1@gmail.com
 */

import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.breezelin.fmlib.widgets.FMTipDialog;
import com.breezelin.freshmarket.R;
import com.breezelin.freshmarket.activities.MainActivity;
import com.breezelin.freshmarket.app.BaseFragment;
import com.breezelin.freshmarket.app.FMApplication;
import com.breezelin.freshmarket.entities.data.UserInfo;
import com.breezelin.freshmarket.network.TaskCanceler;
import com.breezelin.freshmarket.network.asynctasks.UserInfoTask;
import com.breezelin.freshmarket.utils.UserUtil;
import com.squareup.picasso.Picasso;

/**
 * A simple {@link Fragment} subclass.
 */
public class ProfileFragment extends BaseFragment implements View.OnClickListener,
		FMTipDialog.DialogListener {

	/**
	 * 头像
	 */
	private ImageView portraitImg;
	/**
	 * 用户名
	 */
	private TextView nameTxt;
	/**
	 * 用户类型
	 */
	private TextView typeTxt;
	/**
	 * 余额
	 */
	private TextView balanceTxt;
	/**
	 * U币
	 */
	private TextView ubTxt;

	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
	                         Bundle savedInstanceState) {
		rootView = inflater.inflate(R.layout.fragment_profile, container, false);
		bindViews();
		getData();
		return rootView;
	}

	@Override
	public void onDestroy() {
		super.onDestroy();
		TaskCanceler.cancelProfileRequests();
	}

	/**
	 * 绑定视图
	 */
	private void bindViews() {
		portraitImg = (ImageView) rootView.findViewById(R.id.img_portrait);
		nameTxt = (TextView) rootView.findViewById(R.id.txt_name);
		typeTxt = (TextView) rootView.findViewById(R.id.txt_type);
		balanceTxt = (TextView) rootView.findViewById(R.id.txt_balance);
		ubTxt = (TextView) rootView.findViewById(R.id.txt_ub);
		rootView.findViewById(R.id.btn_logout).setOnClickListener(this);
	}

	/**
	 * 获取用户数据
	 */
	private void getData() {
		new UserInfoTask() {

			@Override
			protected void onSuccess(UserInfo userInfo) {
				setUserData(userInfo);
			}

			@Override
			protected void onError() {
				super.onError();
			}
		}.execute();
	}

	/**
	 * 设置用户信息至视图
	 *
	 * @param info 用户信息
	 */
	private void setUserData(UserInfo info) {
		Picasso.with(getActivity())
				.load(info.getUserPhoto())
				.error(R.mipmap.ic_launcher)
				.into(portraitImg);
		nameTxt.setText(info.getUserName());
		typeTxt.setText(info.getUserLevelText());
		balanceTxt.setText(info.getBalance());
		ubTxt.setText(info.getUb());
	}

	/**
	 * 登出操作
	 */
	private void logout() {
		// 重置登录状态
		FMApplication.isLogin = false;
		// 清除用户信息
		UserUtil.clearUserData();
		// 重新启动主页，并加入登出动作
		Intent intent = new Intent(getActivity(), MainActivity.class);
		intent.setAction(MainActivity.ACTION_LOGOUT);
		startActivity(intent);
	}

	@Override
	public void onClick(View v) {
		switch (v.getId()) {
			case R.id.btn_logout: {
				FMTipDialog dialog = new FMTipDialog(getActivity(), getString(R.string.dialog_logout), this);
				dialog.show();
				break;
			}
		}
	}

	@Override
	public void onButtonClick(boolean left) {
		if (!left) {
			logout();
		}
	}
}
